<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/18 0018
 * Time: 上午 9:25
 * Administrator
 */

namespace app\api\controller;


use app\common\model\AccountBook;
use app\common\model\BankList;
use app\common\model\BankMap;
use app\common\service\DateService;
use think\Db;
use think\db\Query;
use think\Exception;

class Bank extends Base
{
    public function getList()
    {
        $data = BankList::all(['userId'=>$this->user->userId]);
        if (!$data){
            return $this->errorInfo('暂无数据');
        }
        return $this->successInfo('成功获取信息',$data);
    }

    public function getCardInfo()
    {
        $id = input('id');
        $bank = BankList::get($id);
        if (!$bank){
            return $this->errorInfo('暂无数据');
        }
        return $this->successInfo('成功获取信息',$bank->getData());
    }

    public function getBankMap()
    {
        $bms = BankMap::all();
        if (!$bms){
            return $this->errorInfo('暂无数据');
        }
        return $this->successInfo('成功获取信息',$bms);
    }

    public function add()
    {
        /** 提交数据
         array (
        'name' => '承德银行',
        'bank_map_id' => '100024',
        'card_number' => '9985',
        'statementDate' => '24',
        'repaymentDate' => '3',
        'user_id' => 'vo460110732579952',
        )
         */
        $data = input();
        $bmid = $data['bank_map_id'];
        $bmModel = BankMap::get($bmid);
        unset($data['bank_map_id']);
        $bank = BankList::create([
            'card_number'=>$data['card_number']
            ,'statementDate'=>$data['statementDate']
            ,'repaymentDate'=>$data['repaymentDate']
            ,'userId'=>$this->user->userId
            ,'remark'=>isset($data['remark'])?$data['remark']:''
            ,'name'=>$bmModel->name
            ,'card_logo'=>$bmModel->logo
        ]);
        if ($bank){
            return $this->successInfo('成功新增银行卡！');
        }
        return $data;
    }

    public function update()
    {
        // {"id":"3","name":"招商银行","bank_map_id":"100175","card_number":"6475","statementDate":"6","repaymentDate":"24","remark":"我的名字","user_id":"vo460110732579952"}
        $id = input('id');
        $bank = BankList::get(['id'=>$id,'userId'=>$this->ue->user_id]);
        $r = $bank->allowField(true)->save(input());
        if (!$r){
            return $this->errorInfo('修改失败');
        }
        return $this->successInfo('已成功修改');
    }

    public function del()
    {
        $id = input('id');
        if (!$id){
            return $this->errorInfo('无权访问');
        }
        $bank = BankList::get(['id'=>$id,'userId'=>$this->user->userId]);
        if (!$bank){
            return $this->errorInfo('非法数据');
        }
        Db::startTrans();
        try{
            $num = AccountBook::destroy(['bid'=>$id,'userid'=>$this->user->userId]);
            if ($num !== false){
                $r = $bank->delete();
                if ($r !== false){
                    Db::commit();
                    return $this->successInfo('已成功删除'.$num.'条账单数据以及'.$r.'条银行卡记录');
                }else{
                    Db::rollback();
                    return $this->errorInfo('删除数据失败');
                }
            }else{
                Db::rollback();
                return $this->errorInfo('删除数据失败');
            }
        }catch (Exception $exception){
            Db::rollback();
            return $this->exceptionInfo($exception);
        }
    }
}